<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
	"http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>Testing dojo._data / NodeList.data</title>
		<script type="text/javascript" src="../dojo.js" data-dojo-config="isDebug:true"></script>
		<script type="text/javascript">
			require(["doh", "dojo/dom-construct", "dojo/query", "dojo/NodeList-data", "dojo/domReady!"],
					function(doh, domConstruct, $, NodeList){
				var len = function(obj){
					var x = 0;
					for(var i in obj){ x++ }
					return x;
				};

				doh.register([
					function sanity(t){

						var list = $("#foo");
						var lis = $("#bar > li");

						t.is(1, list.length, "i'm not insane");
						t.is(2, lis.length, "li's are sane, too");

						list.data("a", "b");
						lis.data("a", "b");

						t.is("b", list.data("a")[0]);
						t.is(["b"], list.data("a"));

						t.is(["b","b"], lis.data("a"));
						t.is("b", lis.data("a")[0]);
					},

					function basicdata(t){

						var list = $('#foo');

						list.data("bar", 6)
							.data("baz", "a")
							.data("bam", [1,2,3])
							.data("foo", { a:"b" })
						;

						var newlist = $("#foo");

						t.is(6, newlist.data("bar")[0]);
						t.is("a", newlist.data("baz")[0]);
						t.is(3, newlist.data("bam")[0].length);
						t.is(1, newlist.data("bam")[0][0]);
						t.is("b", newlist.data("foo")[0].a);

					},

					function hashdata(t){

						$("#foo").data({
							bar:"baz",
							foo:"bap"
						});

						t.is("baz", $('#foo').data("bar")[0]);
						t.is("bap", $('#foo').data("foo")[0]);

					},

					function butdoesitchain(t){

						$("#foo").data("bar", 42).style("color", "red");
						t.is(42, $("#foo").data("bar")[0]);
					},

					function getanobjectback(t){

						$("#foo").data("afoo", 1);
						$("#foo").data("bfoo", 2);

						var obj = $("#foo").data()[0];

						t.is(1, obj.afoo);
						t.is(2, obj.bfoo);

					},

					function plaindata(t){
						$("#bar li").data("bar", 42)
							.forEach(function(n){
								t.is(42, dojo._nodeData(n, "bar"));
							});
					},

					function removeData(t){
						$("#bar li").removeData("bar");
						$("#bar li").forEach(function(n){
							t.t(!dojo._nodeData(n, "bar"));
						});

						$("#bar li").data({
							a:"b", c:"d", e:"f"
						});

						$("#bar li").removeData();
						var data = $("#bar li").data()[0];

						t.f(data.a);
						t.f(data.c);
						t.f(data.e);
					},

					function multidata(t){

						var ret = $("#bar li");
						t.is(2, ret.length, "sanity: 2 (0..1) li's in query");
						ret = ret.data("bar", "baz").data();

						t.is(ret[0].bar, "baz", "item 0 was set");
						t.is(ret[1].bar, "baz", "item 1 was set");

						$("li").at(0).removeData();

						var ret2 = $("#bar li").data();
						t.is(ret.length, 2, "sanity: 2 (0..1) li's in query");
						t.f(ret2[0].bar, "at(0) was removed");
						t.is(ret2[1].bar, "baz", "at(1) was untouched");

					},

					function obj(t){
						var x = $("#foo").data({ bar: { baz:"bam" }}).data("bar");
						t.is("bam", x[0].baz);

					},

					function cleanData(t){

						if(!NodeList._nodeDataCache){
							doh.debug("We must be testing a built Dojo. No access to dataCache");
							return;
						}

						var me = $("#bar li").data("die", "yes");
						me.at(0).attr("id", "killme");
						var data = me.data();

						t.is(2, me.length);
						t.is("yes", me.data("die")[0]);
						t.is("yes", me.data("die")[1]);

						var l = len(NodeList._nodeDataCache);

						domConstruct.destroy("killme");
						NodeList._gcNodeData();

						t.is(l - 1, len(NodeList._nodeDataCache), "one item removed because destroyed");
						me = $("#bar li");
						t.is(1, me.length);

					},

					function literals(t){
						// this is an implementation detail. object literals count,
						// but doesn't mean you should use them.
						var x = { a:1 };

						var one = $("#lit span").data("literal", x);
						x.a++;

						t.is(2, $("#lit span").data("literal")[0].a);
						t.is(2, $("#lit span").data("literal")[1].a);

					},

					function clearall(t){

						if(!NodeList._nodeDataCache){
							doh.debug("We must be testing a built Dojo. No access to dataCache");
							return;
						}

						var l = len(NodeList._nodeDataCache);
						t.t(l, "there is stuff in the cache");

						$("#b > *").forEach(domConstruct.destroy);
						dojo._gcNodeData();
						t.is(0, len(NodeList._nodeDataCache), "no longer stuff in the cache");
					}
				]);

				doh.run();
			});
		</script>
	</head>
	<body>
		<div id="b">
			<div id="foo">woot.</div>
			<ul id="bar">
				<li>baz</li>
				<li>bam</li>
			</ul>
			<p id="lit"><span>hi</span><span>there</span></p>
		</div>
	</body>
</html>
